package com.hbx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbx.model.RoleUser;
import com.hbx.mapper.RoleUserMapper;
import com.hbx.service.RoleUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbx.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hbx
 * @since 2022-01-15
 */
@Service
public class RoleUserServiceImpl extends ServiceImpl<RoleUserMapper, RoleUser> implements RoleUserService {

    @Autowired
    private RoleUserMapper roleUserMapper;

    /**
     * 获得用户的已有角色id
     * @param userId
     * @param r
     */
    @Override
    public void getUserHasRoleId(Integer userId, Result r) {
        QueryWrapper<RoleUser> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",userId);
        List<RoleUser> roleUserList = roleUserMapper.selectList(wrapper);
        List<Integer> ids = roleUserList.stream()
                .map(ru -> ru.getRid())
                .distinct().collect(Collectors.toList());
        r.setCode(200).setData(ids);
    }
}
